﻿/****************************************************************
*   作者：qq918
*   创建时间：2017/12/23 14:56:59
*   描述说明：
*****************************************************************/

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.IO;

namespace Model
{
    [ObjectEvent]
    public class WebServiceComponentEvent : ObjectEvent<WebServiceComponent>, IAwake<string>
    {
        public void Awake(string p1)
        {
            this.Get().Awake(p1);
        }
    }

    /// <summary>
    /// WebService 组件
    /// </summary>
    public class WebServiceComponent : Component
    {
        private  IWebHost webHost;

        public void Awake(string url)
        {
            this.webHost = new WebHostBuilder()
            .UseKestrel()
            .UseUrls(url)
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseStartup<Startup>()
            .Build();

            this.webHost.RunAsync();
        }
    }

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvcWithDefaultRoute();
        }
    }
}